/**************************************************************************
 * @name:  003_选择排序.c
 * @brief: 这是选择排序代码案例
 * @date:  2025/01/02
 * @author: boluzs@163.com
 * @note: None
 * @version: 1.0.0
 * @Copyright (c) 2025 by boluzs@163.com, All Rights Reserved.
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>

void SelectSort(int *buf, int size)
{
    int i = 0, j = 0;
    int temp = 0;
    int min = 0; // 假设第一个元素为最小并记录下来
    for (i = 0; i < size - 1; i++)
    {
        min = i;
        for (j = i + 1; j < size; j++)
        {
            // 需要用最小值元素和序列中的其他元素比较
            if (buf[min] > buf[j])
            {
                min = j;
            }
        }
        // 交换元素
        temp = buf[i];
        buf[i] = buf[min];
        buf[min] = temp;
    }
}

int main()
{
    int *arr = calloc(1, sizeof(int) * 8);
    arr[0] = 3;
    arr[1] = 6;
    arr[2] = 2;
    arr[3] = 1;
    arr[4] = 8;
    arr[5] = 7;
    arr[6] = 5;
    arr[7] = 4;
    SelectSort(arr, 8);
    for (int i = 0; i < 8; i++)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
    free(arr);
    return 0;
}